﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

using Aliphant.Common;
using Aliphant.Mvvm.XEvent.UI;

namespace Aliphant.UI.AIChat.Models.Stock
{
    public class StockDetail : BindableBase
    {
        private DateTime _timeSpan;
        private bool _btime = false;

        public StockDetail()
        {
            _timeSpan = DateTime.Now;
            for (int i = 0; i < 5; i++)
            {
                LevelFiveAsk.Add(new OrderBook("卖" + (5 - i)));
                LevelFiveBid.Add(new OrderBook("买" + (i + 1)));
            }
        }

        public StockDetail(string[] paramArr) : this()
        {
            Update(paramArr);
        }

        public void Update(string[] paramArr)
        {
            if (paramArr.Length < 87)
            {
                Logger.Warning("获取股票数据有缺失");
                return;
            }

            Name = paramArr[1];
            Code = paramArr[2];
            try
            {
                bool bUpd = false;

                NowPrice = double.Parse(paramArr[3]);
                OpenPrice = double.Parse(paramArr[5]);
                HighPrice = double.Parse(paramArr[33]);
                LowPrice = double.Parse(paramArr[34]);
                RiseFall = double.Parse(paramArr[31]);
                Chg = double.Parse(paramArr[32]);

                var volume = double.Parse(paramArr[6]);
                if (volume != Volume)
                {
                    TimeSharingVol = volume - Volume;
                    bUpd = true;
                }
                Volume = double.Parse(paramArr[6]);

                TurnoverRatio = double.Parse(paramArr[38]);
                PriceRange = double.Parse(paramArr[43]);
                VolumeRatio​ = double.Parse(paramArr[49]);
                FFMarketCap = double.Parse(paramArr[44]);
                MarketCap = double.Parse(paramArr[45]);

                LevelFiveAsk[0].Update(paramArr[27], paramArr[28]);    // 卖五
                LevelFiveAsk[1].Update(paramArr[25], paramArr[26]);    // 卖四
                LevelFiveAsk[2].Update(paramArr[23], paramArr[24]);    // 卖三
                LevelFiveAsk[3].Update(paramArr[21], paramArr[22]);    // 卖二
                LevelFiveAsk[4].Update(paramArr[19], paramArr[20]);    // 卖一

                LevelFiveBid[0].Update(paramArr[9], paramArr[10]);     // 买一
                LevelFiveBid[1].Update(paramArr[11], paramArr[12]);    // 买二
                LevelFiveBid[2].Update(paramArr[13], paramArr[14]);    // 买三
                LevelFiveBid[3].Update(paramArr[15], paramArr[16]);    // 买四
                LevelFiveBid[4].Update(paramArr[17], paramArr[18]);    // 买五

                // 计算分时量
                var seconds = (DateTime.Now - _timeSpan).TotalSeconds;
                var nMinute = DateTime.Now.Minute;
                var tMinute = _timeSpan.Minute;

                if (nMinute == tMinute && bUpd)
                {
                    if (_btime)
                    {
                        TimeVolume = Math.Abs(TimeSharingVol);
                        _btime = false;
                    }
                    else
                    {
                        TimeVolume += Math.Abs(TimeSharingVol);
                    }
                }
                else
                {
                    _timeSpan = DateTime.Now;
                    _btime = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        /// <summary>
        /// 股票名称
        /// </summary>
        private string _name = string.Empty;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        private string _code = string.Empty;
        public string Code { get => _code; set => SetProperty(ref _code, value); }

        /// <summary>
        /// 现
        /// </summary>
        private double _nowPrice;
        public double NowPrice { get => _nowPrice; set => SetProperty(ref _nowPrice, value); }

        /// <summary>
        /// 开
        /// </summary>
        private double _openPrice;
        public double OpenPrice { get => _openPrice; set => SetProperty(ref _openPrice, value); }

        /// <summary>
        /// 最高
        /// </summary>
        private double _highPrice;
        public double HighPrice { get => _highPrice; set => SetProperty(ref _highPrice, value); }

        /// <summary>
        /// 最低
        /// </summary>
        private double _lowPrice;
        public double LowPrice { get => _lowPrice; set => SetProperty(ref _lowPrice, value); }

        /// <summary>
        /// 涨跌
        /// </summary>
        private double _riseFall;
        public double RiseFall { get => _riseFall; set => SetProperty(ref _riseFall, value); }

        /// <summary>
        /// 涨跌幅
        /// </summary>
        private double _chg;
        public double Chg { get => _chg; set => SetProperty(ref _chg, value); }

        /// <summary>
        /// 成交量
        /// </summary>
        private double _volume;
        public double Volume { get => _volume; set => SetProperty(ref _volume, value); }

        /// <summary>
        /// 现手
        /// </summary>
        private double _timeSharingVol;
        public double TimeSharingVol { get => _timeSharingVol; set => SetProperty(ref _timeSharingVol, value); }

        /// <summary>
        /// 分时量
        /// </summary>
        private double _timeVolume;
        public double TimeVolume { get => _timeVolume; set => SetProperty(ref _timeVolume, value); }

        /// <summary>
        /// 换手比
        /// </summary>
        private double _turnoverRatio;
        public double TurnoverRatio { get => _turnoverRatio; set => SetProperty(ref _turnoverRatio, value); }

        /// <summary>
        /// 振幅
        /// </summary>
        private double _priceRange;
        public double PriceRange { get => _priceRange; set => SetProperty(ref _priceRange, value); }

        /// <summary>
        /// 量比
        /// </summary>
        private double _volumeRatio​;
        public double VolumeRatio​ { get => _volumeRatio​; set => SetProperty(ref _volumeRatio​, value); }

        /// <summary>
        /// 流通值 Free-Float MarketCap
        /// </summary>
        private double _ffMarketCap;
        public double FFMarketCap { get => _ffMarketCap; set => SetProperty(ref _ffMarketCap, value); }

        /// <summary>
        /// 市值
        /// </summary>
        private double _marketCap;
        public double MarketCap { get => _marketCap; set => SetProperty(ref _marketCap, value); }

        /// <summary>
        /// 五档卖盘
        /// </summary>
        public ObservableCollection<OrderBook> LevelFiveAsk { get; private set; } = [];

        /// <summary>
        /// 五档买盘
        /// </summary>
        public ObservableCollection<OrderBook> LevelFiveBid { get; private set; } = [];

    }

    public class OrderBook​ : BindableBase
    {
        public OrderBook(string pos)
        {
            Name = pos;
        }

        public void Update(string price, string orderSize)
        {
            try
            {
                Price = double.Parse(price);
                OrderSize = double.Parse(orderSize);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private string _name = string.Empty;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        /// <summary>
        /// 价格
        /// </summary>
        private double _price;
        public double Price { get => _price; set => SetProperty(ref _price, value); }

        /// <summary>
        /// 委托单
        /// </summary>
        private double _orderSize;
        public double OrderSize { get => _orderSize; set => SetProperty(ref _orderSize, value); }
    }
}
